/*
 * @Author: 朱安武 549410045@qq.com
 * @Date: 2023-01-09 15:40:43
 * @LastEditors: 朱安武 549410045@qq.com
 * @LastEditTime: 2023-01-10 17:36:10
 * @FilePath: \pinia-todo-setup\src\stores\todo-store.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'

export const useTodoStore = defineStore('todo-store', {
  state() {
    return {
      // 定义todo 元素 { id, content, isFinished }
      todoList: [],
      filterType:'0'
    }
  },
  getters: {
    filterList(state){
      if (state.filterType === '1') {
        return state.todoList.filter(item => item.isFinished === false)
      } else if (state.filterType === '2') {
        return state.todoList.filter(item => item.isFinished === true)
      } else {
        return state.todoList
      }
    }
  },
  actions: {
    addTodo(content) {
      this.todoList.push({
        // 生成不重复的 id： nanoid / uuid
        id: Date.now(),
        content,
        isFinished: false
      })
    },
    finished(id){
      this.todoList.find(item=>item.id === id).isFinished = true
    },
    remove(id){
      const index = this.todoList.findIndex(item=>item.id === id);
      this.todoList.splice(index,1)
    },
    setFilter(type){
      this.filterType = type
    }
  }
})